{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def record(length = 4):\n",
    "    os.system(\"aplay ../../../resources/ding.wav\")    \n",
    "    os.system(\"arecord -d %d -r 16000 -c 1 -t wav -f S16_LE record.wav\" % (length,) )\n",
    "record()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "from aip import AipSpeech\n",
    "import os\n",
    "import yaml\n",
    "\n",
    "with open(\"../../../config.yaml\") as f:\n",
    "    #加载配置文件\n",
    "    config = yaml.load(f)['baidu_yuyin']\n",
    "    client = AipSpeech(config['app_id'], config['api_key'], config['secret_key'])\n",
    "\n",
    "# 识别本地文件\n",
    "def listen_and_recognize(length = 4):\n",
    "    os.system(\"aplay ../../../resources/ding.wav\")    \n",
    "    os.system(\"arecord -d %d -r 16000 -c 1 -t wav -f S16_LE record.wav\" % (length,) )    \n",
    "    file = get_file_content(\"./record.wav\")\n",
    "    with open(\"./record.wav\", 'rb') as fp:\n",
    "        res = client.asr(fp.read(), 'wav', 16000, { 'dev_pid': 1536,})\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'corpus_no': '6684406171391810991',\n",
       " 'err_msg': 'success.',\n",
       " 'err_no': 0,\n",
       " 'result': ['一二三四五六七八九十十一十二十三十四'],\n",
       " 'sn': '620904524851556334591'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "listen_and_recognize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Response [302]>\n",
      "http://m10.music.126.net/20190427212338/f3b21559e0d6a0e116141d18a9c20bf4/ymusic/510f/5659/510b/fe1f2bda3ca3bd68ad48fa2b82f1d7a1.mp3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "import os\n",
    "res = requests.post(url=\"http://music.163.com/song/media/outer/url?id=1357999894\", allow_redirects=False) # 注 allow_redirects=False是必须的\n",
    "print(res)\n",
    "print(res.headers['location']) # 获取其他的也是一样的\n",
    "redi_url = res.headers['location']\n",
    "os.system(\"mpg123 \"+redi_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
